#include "ticket.h"
#include "time.h"
#include "file.h"
#include "list.h"
#include "system.h"
#include "sort.h"
#include "comment.h"
int tId = -1;
extern char userName[20];
TICKET * createTicket()
{
     TICKET * pT = malloc(sizeof(TICKET));
     pT->tId = ++tId;
     pT->count = 20;
     printf("输入票价\n");
     scanf("%f",&pT->price);
     printf("请输入起始城市\n");
     scanf("%s",pT->start);
     printf("请输入到达城市\n");
     scanf("%s",pT->arrival);
     printf("请输入出发时间\n");
     struct tm t;
     memset(&t,0,sizeof(t));
     printf("请输入年份\n");
     scanf("%d",&t.tm_year);
     t.tm_year = t.tm_year - 1900;
     printf("请输入月份\n");
     scanf("%d",&t.tm_mon);
     t.tm_mon = t.tm_mon -1;
     printf("请输入日期\n");
     scanf("%d",&t.tm_mday);  
     printf("请输入小时\n");
     scanf("%d",&t.tm_hour);  
     printf("请输入分钟\n");
     scanf("%d",&t.tm_min);  
     //输入到达时间
     printf("请输入到达时间\n");
     struct tm a;
     memset(&a,0,sizeof(t));
     printf("请输入年份\n");
     scanf("%d",&a.tm_year);
     a.tm_year = a.tm_year - 1900;
     printf("请输入月份\n");
     scanf("%d",&a.tm_mon);
     a.tm_mon = a.tm_mon -1;
     printf("请输入日期\n");
     scanf("%d",&a.tm_mday);  
     printf("请输入小时\n");
     scanf("%d",&a.tm_hour);  
     printf("请输入分钟\n");
     scanf("%d",&a.tm_min);  
     //把时间结构体变为时间戳（整数时间）
     pT->start_time = mktime(&t); //出发时间戳
     pT->arrival_time = mktime(&a); //到达时间戳
     //printf("%ld\n",pT->start_time);
     //把时间戳变为本地时间
     // struct tm * pt = localtime(&pT->start_time);
     // printf("%d-%d-%d %d:%d\n",pt->tm_year+1900,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min);
     return pT;
}

void addTicket(){
    TICKET *ticket= createTicket();
    FILE *pf = fopen("UI/Ticket/tickets","ab+");
    write(pf,ticket,sizeof(TICKET));
    fputc('\n',pf);            //换行
    fclose(pf);
    printf("添加车票成功！按任意键继续\n");
    getchar();
    getchar();
}

void display(){
    FILE *pf = fopen("UI/Ticket/tickets","rb");
    struct node *pHead=createLists(pHead);
    TICKET *ticket = malloc(sizeof(TICKET));
     //读取一个用户的字节数
     int ret = read(pf,ticket,sizeof(TICKET));
     //如果返回值不为0说明读取成功了，否则说明文件已经到了末尾
     while(ret != 0)
     {
          fgetc(pf);
          //把读取的数据地址加入到链表中
          add(pHead,ticket);
          //重新分配一个空间
          ticket = malloc(sizeof(TICKET));
          //继续读取下面的数据
          ret = read(pf,ticket,sizeof(TICKET));
     }
     if(ret == 0)
     {
          free(ticket);
     }
     closeFile(pf);
     struct node * pTemp = pHead->pNext;
     //不为头节点，说明链表没有结束
     printf("编号  出发地    出发时间     目的地      到达时间        价格    余量\n");
     while (pTemp!=pHead)
     { 
          printf("%-3d %8s",((TICKET*)pTemp->pData)->tId,((TICKET*)pTemp->pData)->start);
          struct tm * pt = localtime(&((TICKET*)pTemp->pData)->start_time);
          printf("%9d-%d-%d %02d:%02d",pt->tm_year+1900,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min);
          printf("%8s ",((TICKET*)pTemp->pData)->arrival);
          pt = localtime(&((TICKET*)pTemp->pData)->arrival_time);
          printf("%8d-%d-%d %02d:%02d",pt->tm_year+1900,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min);
          printf("%7.1f %6d \n",((TICKET*)pTemp->pData)->price,((TICKET*)pTemp->pData)->count);
          pTemp = pTemp->pNext;
     }
     printf("查看成功！按任意键继续\n");
    getchar();
    getchar();
}

void deleteTicket(){
     int id,index=0;
     printf("输入删除的车票编号:");
     scanf("%d",&id);
     struct node * pHead = loadListFromFile("UI/Ticket/tickets",sizeof(TICKET));
     struct node * pTemp = pHead->pNext;
     while(pTemp!=pHead){
          if(((TICKET*)pTemp->pData)->tId == id){
               delete(pHead,index);
               saveListToFile("UI/Ticket/tickets",pHead,sizeof(TICKET));
               printf("删除车票成功！按任意键继续\n");
               getchar();
               getchar();
               break;
          }
          pTemp = pTemp->pNext;
          index++;
     }
     printf("没有此张票！\n");
}

void alterTicket(){
     int id,index=0;
     printf("输入修改的车票编号:");
     scanf("%d",&id);
     struct node * pHead = loadListFromFile("UI/Ticket/tickets",sizeof(TICKET));
     struct node * pTemp = pHead->pNext;
     while(pTemp!=pHead){
          if(((TICKET*)pTemp->pData)->tId == id){
                char  pSelect;
                showSelect("UI/admin/alter");
                printf("你选择的操作为：");
                scanf("%s",&pSelect);
                switch (pSelect)
                {
                case '1':
                    printf("修改票价\n");
                    scanf("%f",&((TICKET*)pTemp->pData)->price);
                    break;
                case '2':
                    printf("修改出发地\n");
                    scanf("%s",((TICKET*)pTemp->pData)->start);
                    break;
                case '3':
                    printf("修改目的地\n");
                    scanf("%s",((TICKET*)pTemp->pData)->arrival);
                    break;
                case '4':
                    printf("修改出发时间\n");
                         struct tm t;
     memset(&t,0,sizeof(t));
     printf("请输入年份\n");
     scanf("%d",&t.tm_year);
     t.tm_year = t.tm_year - 1900;
     printf("请输入月份\n");
     scanf("%d",&t.tm_mon);
     t.tm_mon = t.tm_mon -1;
     printf("请输入日期\n");
     scanf("%d",&t.tm_mday);  
     printf("请输入小时\n");
     scanf("%d",&t.tm_hour);  
     printf("请输入分钟\n");
     scanf("%d",&t.tm_min); 
     ((TICKET*)pTemp->pData)->start_time = mktime(&t); //出发时间戳
                    break;
                case '5':
                    printf("修改到达时间\n");
                    struct tm a;
     memset(&a,0,sizeof(a));
     printf("请输入年份\n");
     scanf("%d",&a.tm_year);
     a.tm_year = a.tm_year - 1900;
     printf("请输入月份\n");
     scanf("%d",&a.tm_mon);
     a.tm_mon = a.tm_mon -1;
     printf("请输入日期\n");
     scanf("%d",&a.tm_mday);  
     printf("请输入小时\n");
     scanf("%d",&a.tm_hour);  
     printf("请输入分钟\n");
     scanf("%d",&a.tm_min); 
     ((TICKET*)pTemp->pData)->arrival_time = mktime(&a); //到达时间戳
                    break;   
                default:
                    break;
                }
               saveListToFile("UI/Ticket/tickets",pHead,sizeof(TICKET));
               printf("修改车票成功！按任意键继续\n");
               getchar();
               getchar();
               return;
          }
          pTemp = pTemp->pNext;
          index++;
     }
     printf("没有此张票！\n");
}
void buyTicket(){
     display();
     int id;
     printf("输入要购买的车票编号:");
     scanf("%d",&id);
     struct node * pHead = loadListFromFile("UI/Ticket/tickets",sizeof(TICKET));
     struct node * pTemp = pHead->pNext;
     while(pTemp!=pHead){
          if(((TICKET*)pTemp->pData)->tId == id){
               char p[20] = "user_data/";
               strcat(p,userName);
               char * pa = p;
               struct node * ptHead = loadListFromFile(pa,sizeof(UT));
               struct node * ptTemp = ptHead->pNext;
               FILE * pf = fopen(pa,"wb");
                    UT * ut = malloc(sizeof(UT));
                    // add(ptHead,ut);          待写：多买几张
                    // if (ptHead->pNext==p)
                    // {
                    //      /* code */
                    // }
                    ((UT*)ptTemp->pData)->ticket=((TICKET*)pTemp->pData);

               
               printf("%f\n",(((UT*)ptTemp->pData)->cash));
               if ((((UT*)ptTemp->pData)->cash)>=((TICKET*)pTemp->pData)->price&&((TICKET*)pTemp->pData)->count>0)
               {
               (((UT*)ptTemp->pData)->cash) = (((UT*)ptTemp->pData)->cash)-((TICKET*)pTemp->pData)->price;
              saveListToFile(pa,ptHead,sizeof(UT)); 
               ((TICKET*)pTemp->pData)->count-- ;
               saveListToFile("UI/Ticket/tickets",pHead,sizeof(TICKET));
               printf("购买车票成功！按任意键继续\n");
               getchar();
               getchar();
               break;
               }else{
                    printf("余额不足,购买失败！\n");
                    break;
               }
          }
          pTemp = pTemp->pNext;
     }

}

void budget(){
          char p[20] = "user_data/";
               strcat(p,userName);
               char * pa = p;
               UT * ut = malloc(sizeof(UT));
               FILE * pf = openFile(pa,"ab+");
               read(pf,ut,sizeof(UT));
               printf("当前用户：%s\n",userName);
               printf("余额：%f\n",ut->cash);
               printf("最近一次购买车票信息\n");
               printf("编号   出发地    出发时间     目的地    到达时间       价格 \n");
               if (ut->cash!=200)
               {
          printf("%-3d %8s",((TICKET*)ut->ticket)->tId,((TICKET*)ut->ticket)->start);
          struct tm * pt = localtime(&((TICKET*)ut->ticket)->start_time);
          printf("%11d-%d-%d %d:%d",pt->tm_year+1900,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min);
          printf("%7s ",((TICKET*)ut->ticket)->arrival);
          pt = localtime(&((TICKET*)ut->ticket)->arrival_time);
          printf("%9d-%d-%d %d:%d",pt->tm_year+1900,pt->tm_mon+1,pt->tm_mday,pt->tm_hour,pt->tm_min);
          printf("%7.1f \n",((TICKET*)ut->ticket)->price);   
               }            
}

void cz(){
     char p[20] = "user_data/";
               strcat(p,userName);
               char * pa = p;
               UT * ut = malloc(sizeof(UT));
               struct node * ptHead = loadListFromFile(pa,sizeof(UT));
               struct node * ptTemp = ptHead->pNext;
               FILE * pf = openFile(pa,"wb+");
               int cash ;
               printf("输入充值金额：\n");
               scanf("%d",&cash);
               // ((UT*)ptTemp->pData)->ticket=((TICKET*)ptTemp->pData);
               ((UT*)ptTemp->pData)->cash+=cash;
               saveListToFile(pa,ptHead,sizeof(UT));
}


// void sortPrice(){
//      struct node * pHead = loadListFromFile("UI/Ticket/tickets",sizeof(TICKET));
//      struct node * pTemp = pHead->pNext;
//      int len = 0;
//      TICKET ** t ;
//      while (pTemp!=pHead)
//      {
//           *(t+len*sizeof(TICKET)) = pTemp->pData;
//           len++;
//           pTemp = pTemp->pNext;
//      }
//      selectSort(t,len,sizeof(TICKET),comparePrice,1);
//      struct node * ppTemp = pHead->pNext;
//      for (int i = 0; i < len; i++)
//      {
//         ppTemp->pData =  *(t+len*sizeof(TICKET)) ;
//         ppTemp = ppTemp->pNext;
//      }
//           saveListToFile("UI/Ticket/tickets",pHead,sizeof(TICKET));
// }